package com.franzliszt.OrderOnline.util;

import android.content.Context;
import android.graphics.Color;

import com.franzliszt.OrderOnline.SQLite.Dao;
import com.franzliszt.OrderOnline.tool.GoodsList;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.formatter.ColumnChartValueFormatter;
import lecho.lib.hellocharts.formatter.SimpleColumnChartValueFormatter;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.view.ColumnChartView;

public class ChartTool {
    private Dao dao;
    private Context context;
    private List<GoodsList> goodsLists = new ArrayList<>();
    private ColumnChartData columnData;
    //底部标题
    private List<String> title = new ArrayList<>();
    //颜色值
    private List<Integer> color = new ArrayList<>();
    //X、Y轴值list
    private List<AxisValue> axisXValues = new ArrayList<>();
    private List<SubcolumnValue> mPointValues;
    //所有的柱子
    private List<Column> columns = new ArrayList<>();
    private int Totalnum;
    private int single = 1;
    public void Init(Context context){
        this.context = context;
        dao = new Dao(context);
        goodsLists = dao.QueryAll();
        Totalnum = goodsLists.size();
    }

    public void setChartViewData(ColumnChartView chart) {

        for (int i = 0; i <goodsLists.size() ; i++) {
            title.add(goodsLists.get(i).getName());
        }

        for (int i = 0; i < Totalnum; i++) {
            if (i % 2 == 0)
            color.add(Color.parseColor("#009BDB"));
            else
            color.add(Color.parseColor("#85B74F"));
        }
        for (int j = 0; j < Totalnum ; j++) {
            mPointValues = new ArrayList<>();
            for (int i = 0; i < single; i++) {
                mPointValues.add(new SubcolumnValue((goodsLists.get(j).getPrice()), color.get(j)));
                //值的大小、颜色
                //设置X轴的柱子所对应的属性名称(底部文字)
                axisXValues.add(new AxisValue(j).setLabel(title.get(j)));
            }
            Column column = new Column(mPointValues);
            ColumnChartValueFormatter chartValueFormatter = new SimpleColumnChartValueFormatter(2);
            column.setFormatter(chartValueFormatter);
            column.setHasLabelsOnlyForSelected(false);
            column.setHasLabels(true);
            //column.setValues(mPointValues);
            //将每个属性得列全部添加到List中
            //添加了7个大柱子Column,单个大柱子里面mPointValues大小为3（自行调整)
            columns.add(column);
        }
        //设置每个柱子的Lable是否选中，为false，表示不用选中，一直显示在柱子上

        //设置Columns添加到Data中
        ColumnChartData columnData = new ColumnChartData(columns);

        //底部
        Axis axisBottom = new Axis(axisXValues);
        //是否显示X轴的网格线
        axisBottom.setHasLines(false);
        //分割线颜色
        axisBottom.setLineColor(Color.parseColor("#ff0000"));
        //字体颜色
        axisBottom.setTextColor(Color.parseColor("#666666"));
        //字体大小
        axisBottom.setTextSize(10);
        //底部文字
        axisBottom.setName("商品名称");
        //每个柱子的便签是否倾斜着显示
        axisBottom.setHasTiltedLabels(true);
        //距离各标签之间的距离,包括离Y轴间距 (0-32之间)
        axisBottom.setMaxLabelChars(10);
        //设置是否自动生成轴对象,自动适应表格的范围(设置之后底部标题变成0-5)
        //axisBottom.setAutoGenerated(true);
        axisBottom.setHasSeparationLine(true);
        //设置x轴在底部显示
        columnData.setAxisXBottom(axisBottom);

        //左边  属性与上面一致
        Axis axisLeft = new Axis();
        axisLeft.setHasLines(false);
        axisLeft.setName("商品单价");
        axisLeft.setHasTiltedLabels(true);
        axisLeft.setTextColor(Color.parseColor("#666666"));
        columnData.setAxisYLeft(axisLeft);

        //设置组与组之间的间隔比率,取值范围0-1,1表示组与组之间不留任何间隔
        columnData.setFillRatio(0.7f);
        chart.setInteractive(false);
        //最后将所有值显示在View中
        chart.setColumnChartData(columnData);
    }
}
